﻿using Common.Contract;
using Common.Contract.Authentication;
using Common.Contract.Authentication.Record;
using Common.Contract.Authentication.Request;
using Common.Contract.Authentication.Response;
using Common.Contract.Paging;
using Common.Contract.SystemConfig.Record;
using Common.Data.Repository.Enum;
using Common.Service;
using Customer360AutoJob.Core.Configuration;
using Customer360AutoJob.Jobs;
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Customer360AutoJob.Job.Email
{
    public class ReturnResultCloseDaoRequest : JobBase, IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            List<CloseDaoRequestRecord> listRequestNeedSendMail = RetrieveListCreateDaoRequest();
            if (listRequestNeedSendMail.Count > 0)
            {
                string mailSubject = "Khoi tao DAO thanh cong";
                listRequestNeedSendMail.ForEach(x =>
                {
                    List<string> listCC = new List<string>();
                    if (!string.IsNullOrEmpty(x.Pic))
                    {
                        listCC.Add(x.Pic + "@vpbank.com.vn");
                    }
                    EmailContent content = new EmailContent
                    {
                        Subject = mailSubject,
                        Body = string.Format("Dear anh/chị. {0}Yêu cầu đã được xử lý, DAO của anh/chị {1} là: {2}. Anh/ chị vui lòng kiểm tra lại trên T24 và báo lại phòng QTDL nếu có trường hợp nào xử lý sai hoặc thiếu. Khi đó, phòng QTDL sẽ liên hệ với IT để kiểm tra lại. {3}Nếu sau khi kiểm tra và đúng với yêu cầu, anh/ chị vui lòng truy cập vào link sau để đóng yêu cầu{4}Trân trọng cảm ơn! ", Environment.NewLine, x.FullName, x.Dao, Environment.NewLine, Environment.NewLine),
                        CC = listCC,
                        To = x.Email
                    };
                    if (SendEmail(content))
                    {
                        x.Status = (int)StatusCloseDaoEnum.SendEmail;
                    }
                });
                var listNeedUpdateStatus = listRequestNeedSendMail.Where(x => x.Status == (int)StatusCloseDaoEnum.SendEmail).ToList();
                UpdateStatusAfterSendEmail(listNeedUpdateStatus);
            }
            List<CloseDaoRequestRecord> listRequestRejectedNeedSendMail = RetrieveListCreateDaoRequestRejected();
            if (listRequestRejectedNeedSendMail.Count > 0)
            {
                string mailSubject = "Khoi tao DAO bi tu choi";
                listRequestRejectedNeedSendMail.ForEach(x =>
                {
                    if (!string.IsNullOrEmpty(x.RejectReason))
                    {

                        List<string> listCC = new List<string>();
                        if (!string.IsNullOrEmpty(x.Pic))
                        {
                            listCC.Add(x.Pic + "@vpbank.com.vn");
                        }
                        EmailContent content = new EmailContent
                        {
                            Subject = mailSubject,
                            Body = string.Format("Kính gửi anh/ chị {0}, {1} Hiện tại yêu cầu của anh/ chị bị từ chối do {2}.Kính đề nghị anh/ chị thực hiện khởi tạo lại yêu cầu. {3}Trân trọng", x.FullName, Environment.NewLine, x.RejectReason, Environment.NewLine),
                            CC = listCC,
                            To = x.Email
                        };
                        if (SendEmail(content))
                        {
                            x.Status = (int)StatusCloseDaoEnum.SendRejectEmail;
                        }
                    }
                });
                var listNeedUpdateStatus = listRequestRejectedNeedSendMail.Where(x => x.Status == (int)StatusCloseDaoEnum.SendRejectEmail).ToList();
                UpdateStatusAfterSendEmail(listNeedUpdateStatus);
            }
        }

        private void UpdateStatusAfterSendEmail(List<CloseDaoRequestRecord> listRequestNeedSendMail)
        {
            using (ServiceClient<IAuthentication> serviceClient = new ServiceClient<IAuthentication>())
            {
                UserRecord CurrentUserRecord = new UserRecord
                {
                    UserName = "cucnt3",
                    GroupIdentifier = "1"
                };
                UpdateCloseDaoRequestRequest requestUpdate = new UpdateCloseDaoRequestRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.DaoManager.ApproveRequestCreateDao"
                    },
                    //Record = userRequest,
                    CurrentUser = CurrentUserRecord,
                    ListCloseDaoRequest = listRequestNeedSendMail
                };
                var result = serviceClient.Proxy.UpdateStatusListCloseDaoRequest(requestUpdate);
            }
        }
        private List<CloseDaoRequestRecord> RetrieveListCreateDaoRequest()
        {
            RetrieveListCloseDaoRequestResponse result = new RetrieveListCloseDaoRequestResponse();

            FilterRecord filter = new FilterRecord
            {
                CreateDaoRequestStatus = (int)StatusCloseDaoEnum.Done
            };

            UserRecord CurrentUserRecord = new UserRecord
            {
                UserName = "cucnt3",
                GroupIdentifier = "1"
            };
            using (ServiceClient<IAuthentication> serviceClient = new ServiceClient<IAuthentication>())
            {

                RetrieveListCloseDaoRequestRequest request = new RetrieveListCloseDaoRequestRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.DaoManagerController.RetrieveListCreateDaoRequest"
                    },
                    Page = new PageRecord
                    {
                        PageSize = 20,
                        PageIndex = 0
                    },
                    CurrentUser = CurrentUserRecord,
                    Filter = filter
                };
                result = serviceClient.Proxy.RetrieveListCloseDaoRequest(request);
            }
            var listRecord = (result == null || result.Records == null) ? new List<CloseDaoRequestRecord>() : result.Records;
            return listRecord;
        }
        private List<CloseDaoRequestRecord> RetrieveListCreateDaoRequestRejected()
        {
            RetrieveListCloseDaoRequestResponse result = new RetrieveListCloseDaoRequestResponse();

            FilterRecord filter = new FilterRecord
            {
                CreateDaoRequestStatus = (int)StatusCloseDaoEnum.Reject
            };

            UserRecord CurrentUserRecord = new UserRecord
            {
                UserName = "cucnt3",
                GroupIdentifier = "1"
            };
            using (ServiceClient<IAuthentication> serviceClient = new ServiceClient<IAuthentication>())
            {

                RetrieveListCloseDaoRequestRequest request = new RetrieveListCloseDaoRequestRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.DaoManagerController.RetrieveListCreateDaoRequest"
                    },
                    Page = new PageRecord
                    {
                        PageSize = 20,
                        PageIndex = 0
                    },
                    CurrentUser = CurrentUserRecord,
                    Filter = filter
                };
                result = serviceClient.Proxy.RetrieveListCloseDaoRequest(request);
            }
            var listRecord = (result == null || result.Records == null) ? new List<CloseDaoRequestRecord>() : result.Records;
            return listRecord;
        }
    }
}